package com.ruoyi.app.service.impl;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.app.aspect.PermissionAspect;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.dto.ArticleDto;
import com.ruoyi.app.dto.LoginUserDto;
import com.ruoyi.app.dto.MemberDto;
import com.ruoyi.app.mapper.*;
import com.ruoyi.app.service.HomeService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/*
@description:
@ClassName HomeServiceImpl
@author chen
@create 2025-03-13 23:50
@Version 1.0
*/
@Slf4j
@Service
public class HomeServiceImpl implements HomeService
{
    @Resource
    @Qualifier
    private VideoInfoMapper videoInfoMapper;
    @Resource
    @Qualifier
    private PictureInfoMapper pictureInfoMapper;
    @Resource
    @Qualifier
    private ArticleInfoMapper articleInfoMapper;
    @Resource
    @Qualifier
    private TripInfoMapper tripInfoMapper;
    @Resource
    @Qualifier
    private ArticleThumbsMapper articleThumbsMapper;
    @Resource
    @Qualifier
    private ArticleCommentMapper articleCommentMapper;
    @Resource
    @Qualifier
    private TripThumbsMapper tripThumbsMapper;
    @Resource
    @Qualifier
    private TripCommentMapper tripCommentMapper;

    /*
     * @==============History===============<br/>
     * @Description //   获取首页视频信息
     * @Date 2025-3-13 23:52
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @Override
    public List<VideoInfo> getVideoInfo(Map<String, Object> params)
    {
        VideoInfoExample videoInfoExample = new VideoInfoExample();
        videoInfoExample.createCriteria().andDisableEqualTo(false);
        return videoInfoMapper.selectByExample(videoInfoExample);
    }

    /*
     * @==============History===============<br/>;
     * @Description //   获取首页图片信息
     * @Date 2025/3/14
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @Override
    public List<PictureInfo> getPictureInfo(Map<String, Object> params)
    {
        PictureInfoExample pictureInfoExample = new PictureInfoExample();
        pictureInfoExample.createCriteria().andDisableEqualTo(false);
        pictureInfoExample.setOrderByClause("is_up,sort desc");
        return pictureInfoMapper.selectByExample(pictureInfoExample);
    }


    /*
     * @==============History===============<br/>;
     * @Description //   小程序获取文章列表
     * @Date 2025/3/18
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @Override
    public List<ArticleDto> getArticleInfoList(Map<String, Object> params)
    {
        List<ArticleDto> articleInfoList = articleInfoMapper.getArticleInfoList(params);
        if(null != articleInfoList && articleInfoList.size() > 0)
        {
            for (ArticleDto articleDto : articleInfoList)
            {
                String articlePicture = articleDto.getArticlePicture();
                if(StringUtils.isNotEmpty(articlePicture))
                {
                    String[] split = articlePicture.split(",");
                    articleDto.setArticlePictureList(Arrays.asList(split));
                }
            }
        }
        return articleInfoList;
    }

    /*
     * @==============History===============<br/>;
     * @Description //   获取文章详情
     * @Date 2025/3/18
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @Override
    public ArticleInfo getArticleInfo(Map<String, Object> params)
    {
        Long articleId = Convert.toLong(params.get("id"));
        Long userId = Convert.toLong(params.get("userId"));
        ArticleInfo articleInfo = articleInfoMapper.getArticleInfo(articleId);

        String articlePicture = articleInfo.getArticlePicture();
        if (StringUtils.isNotEmpty(articlePicture))
        {
            String[] split = articlePicture.split(",");
            articleInfo.setArticlePictureList(Arrays.asList(split));
        }

        articleInfo.setIsThumbs(false);

        List<ArticleThumbs> articleThumbs = articleThumbsMapper.selectList(articleId);

        if(null != articleThumbs && articleThumbs.size() > 0)
        {
            articleInfo.setThumbList(articleThumbs);
            if(null != params.get("userId"))
            {
                long count = articleThumbs.stream().filter(item -> Objects.equals(item.getCreateId(), userId)).count();
                if(count > 0)
                {
                    articleInfo.setIsThumbs(true);
                }
            }
        }

        List<ArticleComment> articleComments = articleCommentMapper.selectList(articleInfo.getId());
        if(null != articleComments && articleComments.size() > 0)
        {
            List<ArticleComment> collect = articleComments.stream()
                    // 过滤出当前文章的的一级评论
                    .filter(item -> item.getParentId() == 0)
                    // 给当前一级评论设置子评论
                    .peek(item -> item.setChildList(getChildren(item,articleComments)))
                    .collect(Collectors.toList());
            articleInfo.setCommentList(collect);
        }
        return articleInfo;
    }


    /*
     * @==============History===============<br/>
     * @Description //   返回root评论的子评论,子评论还有评论继续返回
     * @Date 2025-3-24 21:44
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.util.List<com.ruoyi.app.domain.ArticleComment>
     */
    private  List<ArticleComment> getChildren(ArticleComment root,List<ArticleComment> all)
    {
        return all.stream()
                // filter过滤出当前一级分类的子分类
                .filter(item -> Objects.equals(item.getParentId(), root.getId()))
                // map 给这个收集好的子分类,如果这个子分类还有子分类,递归设置子分类
                .peek(item -> item.setChildList(getChildren(item, all)))
                .collect(Collectors.toList());
    }


    /*
     * @==============History===============<br/>
     * @Description //   小程序获取行程列表
     * @Date 2025-3-29 19:43
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.util.List<com.ruoyi.app.domain.TripInfo>
     */
    @Override
    public List<TripInfo> getTripInfoList(Map<String, Object> params)
    {
        if(StringUtils.isNotEmpty(Convert.toStr(params.get("type"))))
        {
            LoginUserDto loginUserDto = PermissionAspect.loginUser.get();
            MemberDto memberInfo = loginUserDto.getMemberInfo();
            if(null == memberInfo)
            {
                throw new BaseException("当前内容请开通会员进行观看!");
            }
            if("0".equals(Convert.toStr(params.get("type"))))
            {
                // 国外看所有,国内会员只能看国内的
                String memberName = memberInfo.getMemberName();
                if(!"海外会员".equals(memberName))
                {
                    throw new BaseException("当前内容请海外会员进行观看!");
                }
            }
        }

        List<TripInfo> tripInfoList = tripInfoMapper.getTripInfoList(params);
        if(null != tripInfoList && tripInfoList.size() > 0)
        {
            for (TripInfo tripInfo : tripInfoList)
            {
                String tripPicture = tripInfo.getTripPicture();
                if(StringUtils.isNotEmpty(tripPicture))
                {
                    String[] split = tripPicture.split(",");
                    tripInfo.setTripPictureList(Arrays.asList(split));
                }
                String temp3 = tripInfo.getTemp3();
                if(StringUtils.isNotEmpty(temp3))
                {
                    tripInfo.setTemp3Obj(JSON.parseObject(temp3));
                }
                String tripContent = tripInfo.getTripContent();
                if(null != tripContent)
                {
                    tripInfo.setTripContentObj(JSON.parseObject(tripContent));
                }
            }
        }
        return tripInfoList;
    }

    /*
     * @==============History===============<br/>
     * @Description //   获取行程详情信息
     * @Date 2025-3-24 23:35
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.app.domain.TripInfo
     */
    @Override
    public TripInfo getTripInfo(Map<String, Object> params)
    {
        Long tripId = Convert.toLong(params.get("id"));
        Long userId = Convert.toLong(params.get("userId"));
        TripInfo tripInfo = tripInfoMapper.getTripInfo(tripId);


        String tripPicture = tripInfo.getTripPicture();
        if (StringUtils.isNotEmpty(tripPicture))
        {
            String[] split = tripPicture.split(",");
            tripInfo.setTripPictureList(Arrays.asList(split));
        }
        if(StringUtils.isNotEmpty(tripInfo.getTemp1()))
        {
            tripInfo.setIpAddr(IpUtils.getIpAddress(ServletUtils.getRequest()));
        }

        if(StringUtils.isNotEmpty(tripInfo.getTripContent()))
        {
            tripInfo.setTripContentObj(JSON.parseObject(tripInfo.getTripContent()));
        }

        tripInfo.setIsThumbs(false);

        List<TripThumbs> tripThumbList = tripThumbsMapper.selectList(tripId);

        if(null != tripThumbList && tripThumbList.size() > 0)
        {
            tripInfo.setThumbList(tripThumbList);
            if(null != userId)
            {
                long count = tripThumbList.stream().filter(item -> Objects.equals(item.getCreateId(), userId)).count();
                if(count > 0)
                {
                    tripInfo.setIsThumbs(true);
                }
            }
        }

        List<TripComment> tripComments = tripCommentMapper.selectList(tripInfo.getId());
        if(null != tripComments && tripComments.size() > 0)
        {
            List<TripComment> collect = tripComments.stream()
                    // 过滤出当前文章的的一级评论
                    .filter(item -> item.getParentId() == 0)
                    // 给当前一级评论设置子评论
                    .peek(item -> item.setChildList(getChildren(item, tripComments)))
                    .collect(Collectors.toList());
            tripInfo.setCommentList(collect);
        }

        return tripInfo;
    }


    private  List<TripComment> getChildren(TripComment root,List<TripComment> all)
    {
        return all.stream()
                // filter过滤出当前一级分类的子分类
                .filter(item -> Objects.equals(item.getParentId(), root.getId()))
                // map 给这个收集好的子分类,如果这个子分类还有子分类,递归设置子分类
                .peek(item -> item.setChildList(getChildren(item, all)))
                .collect(Collectors.toList());
    }
}
